﻿using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Text;
using System.Web;
using System.Web.Configuration;
using System.Web.Services;

using com.vs.DataAccess;
using com.vs.DataAccess.DO;
using com.vs.General;
using com.vs.General.BL;
using Session = com.vs.General.Session;

namespace com.vs.WebService
{
    /// <summary>
    /// Summary description for LoginService
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
    // [System.Web.Script.Services.ScriptService]
    public class LoginService : System.Web.Services.WebService
    {

        [WebMethod]
        public string GetEnvironmentList(string stUser, string stPswd, string stCompany)
        {
            //Session usrSess = null;
            Session sysSess = null;
            LoginManager lgMgr = null;
            string stErr = null;
            int iUserNum = 0;
            DataTable dtEnv = null;
            StringBuilder sbXML = null;

            try
            {
                sysSess = new Session();
                sysSess.ConnectionString = WebConfigurationManager.ConnectionStrings["systemDB"].ConnectionString;
                lgMgr = new LoginManager();
                stErr = lgMgr.Login(sysSess, stUser, stPswd, stCompany);

                if (StringManager.IsErrorMessage(stErr))
                    return stErr;
                else
                {
                    iUserNum = Int32.Parse(stErr);

                    //获取环境列表
                    dtEnv = lgMgr.GetEnvironmentList(sysSess, iUserNum);
                    //每个公司强制必须至少有一个环境

                    if (dtEnv.Rows.Count >= 1)
                    {
                        dtEnv.TableName = "ENV";
                        dtEnv.DataSet.DataSetName = "ENVS";
                        sbXML = new StringBuilder();
                        dtEnv.DataSet.WriteXml(new StringWriter(sbXML));
                        return sbXML.ToString();
                    }
                    else
                        return MessageManager.MSG_GEN_003; //请联系管理员为您建立初始环境

                    /*
                    usrSess = new Session();
                    usrSess.User = stUser;
                    usrSess.ConnectionString = DataManager.GetString(dtEnv.Rows[0], "CONN_STRING");

                    Application.Lock();
                    Application.Add(usrSess.SessionID.ToString(), usrSess);
                    Application.UnLock();

                    return usrSess.SessionID.ToString();
                    */
                }
            }
            catch (Exception e)
            {
                return MessageManager.MSG_GEN_000 + e.Message ;
            }
        }

        [WebMethod]
        public string Login(string stUser, string stPswd, string stCompany, int iEnvNum)
        {
            Session usrSess = null;
            Session sysSess = null;
            LoginManager lgMgr = null;
            string stErr = null;
            string stConnString = null;

            try
            {
                //context.Response.Write("test");
                sysSess = new Session();
                sysSess.ConnectionString = WebConfigurationManager.ConnectionStrings["systemDB"].ConnectionString;
                lgMgr = new LoginManager();
                stErr = lgMgr.Login(sysSess, stUser, stPswd, stCompany);
                //context.Response.Write("step 0.1");
                if (StringManager.IsErrorMessage(stErr))
                    return stErr;

                //context.Response.Write("step 0.2");
                //for (int i = 0; i < Application.Count; i++)
                //{
                //    Context.Response.Write("step 0.3");
                //    usrSess = Application[i] as Session;
                //    Context.Response.Write("step 0.4");
                //    if (StringManager.Equals(usrSess.User, stUser))
                //    {
                //        Application.Remove(usrSess.SessionID.ToString());
                //        break;
                //    }
                //}
                //context.Response.Write("step 1");
                stConnString = lgMgr.GetEnvConnString(sysSess, iEnvNum);
                //context.Response.Write("step 1.5");
                usrSess = new Session();
                //context.Response.Write("step 1,6");
                usrSess.User = stUser;
                usrSess.ConnectionString = stConnString;
                //context.Response.Write("step 2");
                Application.Lock();
                Application.Add(usrSess.SessionID.ToString(), usrSess);
                //context.Response.Write("step 3");
                Application.UnLock();
                //context.Response.Write("step 4");
                return usrSess.SessionID.ToString();

            }
            catch (Exception e)
            {
                return MessageManager.MSG_GEN_000 + e.Message;
            }
        }

        [WebMethod]
        public string Logout(string stSessID)
        {
            try
            {
                Application.Lock();
                Application.Remove(stSessID);
                Application.UnLock();
                return null;
            }
            catch (Exception)
            {
                return null;
            }
        }

        [WebMethod]
        public string GetDBConnection(string stSessID)
        {
            Session sess = null;

            try
            {
                sess = (Session)Application.Get(stSessID);
                if (null == sess)
                    return null;
                return sess.ConnectionString;
            }
            catch (Exception e)
            {
                ExceptionManager.LogException(sess, e);
                return null;
            }
        }
    }
}
